package cn.aran.org.controller;

import cn.aran.basic.annotation.PreAuthorize;
import cn.aran.basic.utils.AjaxResult;
import cn.aran.basic.utils.PageList;
import cn.aran.org.domain.Employee;
import cn.aran.org.query.EmployeeQuery;
import cn.aran.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "员工的API",description="员工相关的CRUD功能")
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
    * @Title: loadById
    * @Description: 根据ID查询员工
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/26 14:55
    * @Parameters: [id]
    * @Return cn.aran.org.domain.Employee
    */
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Employee loadById(@ApiParam(value="主键ID属性",required = true)  @PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    /**
    * @Title: loadAll
    * @Description: 查询员工所有
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/26 14:55
    * @Parameters: []
    * @Return java.util.List<cn.aran.org.domain.Employee>
    */
    @ApiOperation(value = "查询全部" )
    @GetMapping
    public List<Employee> loadAll(){
        List<Employee> employees = employeeService.loadAll();
        return employees;
    }

    /**
    * @Title: remove
    * @Description: 根据id删除员工
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/26 14:56
    * @Parameters: [id]
    * @Return cn.aran.basic.util.AjaxResult
    */
    @ApiOperation(value = "员工删除" )
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除员工", sn = "department:delete")
    public AjaxResult remove(@ApiParam(value="主键ID属性",required = true)  @PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }

    }

    /**
    * @Title: batchRemove
    * @Description: 员工批量删除
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/26 14:56
    * @Parameters: [ids]
    * @Return cn.aran.basic.util.AjaxResult
    */
    @PatchMapping
    @PreAuthorize(name = "批量删除员工", sn = "employ:patch")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }

    }

    /**
    * @Title: saveOrUpt
    * @Description: 新增修改员工
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/26 14:56
    * @Parameters: [employee]
    * @Return cn.aran.basic.util.AjaxResult
    */
    @ApiOperation(value = "员工新增或修改" )
    @PutMapping
    @PreAuthorize(name = "保存员工", sn = "employee:save")
    public AjaxResult saveOrUpt(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }
    
    /**
    * @Title: queryData
    * @Description: 员工高级查询,分页
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/26 14:56
    * @Parameters: [employeeQuery]
    * @Return cn.aran.basic.util.PageList<cn.aran.org.domain.Employee>
    */
    @ApiOperation(value = "员工高级查询和分页" )
    @PostMapping
    @PreAuthorize(name = "列表员工", sn = "employee:list")
    public PageList<Employee> queryData(@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> data = employeeService.queryData(employeeQuery);
        return data;
    }

    /**
    * @Title: loadByShopId
    * @Description: 获取店铺员工
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 22:50
    * @Parameters: [shopId]
    * @Return java.util.List<cn.aran.org.domain.Employee>
    */
    @GetMapping("/list/{shopId}")
    public List<Employee> loadByShopId(@PathVariable("shopId") Long shopId){
        return employeeService.loadByShopId(shopId);
    }
}
